﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using KaoQin.Common;
using KaoQin.Model;
using KaoQin.BLL;

namespace KaoQin.WPFApp.kaoQin
{
    /// <summary>
    /// GoOutMgr.xaml 的交互逻辑
    /// </summary>
    public partial class GoOutMgr : Window
    {
        public GoOutMgr()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.initData();
        }
        private void initData()
        {
            try
            {
                this.bindList();
                this.cbLeaveType.ItemsSource = StaticDataHelper.GetGoOutType();
                this.cbLeaveType.SelectedIndex = 0;

            }
            catch (Exception ex)
            {

            }
        }

        private void bindList()
        {
            lstEmployee.ItemsSource = new Employee().GetModelList("KName like '%" + txtKey.Text.Trim() + "%'");
        }

        private void bindGoOutList()
        {
            EmployeeInfo empInfo = (EmployeeInfo)lstEmployee.SelectedItem;
            if (empInfo != null)
            {
                int recordCount;
                lsvInfo.ItemsSource = new BLL.GoOutMgr().GetListByPage(" where EmployeeID='" + empInfo.ID + "'", "CreateTime desc", pager.PageIndex, pager.PageSize, out recordCount).Tables[0].DefaultView;
                pager.TotalCount = recordCount;
            }

        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            this.bindList();
        }

        private void lstEmployee_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            bindGoOutList();
        }

        private void DataPager_PageChanged(object sender, DataPager.PageChangedEventArgs e)
        {
            bindGoOutList();

        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MessageBoxResult result = MessageBox.Show("确定删除吗？", "确定", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    string id = (sender as Button).Tag.ToString();
                    new BLL.GoOutMgr().Delete(id);
                    MessageBox.Show("删除成功");

                    bindGoOutList();
                }



            }
            catch (Exception ex)
            {
                MessageBox.Show("删除失败:" + ex.Message);
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                EmployeeInfo empInfo = (EmployeeInfo)lstEmployee.SelectedItem;
                if (string.IsNullOrEmpty(txtBeginDate.Text))
                {
                    MessageBox.Show("请选择外出开始日期");
                    return;
                }
                if (txtBeginTime.Value == null)
                {
                    MessageBox.Show("请选择外出开始时间");
                    return;
                }

                if (string.IsNullOrEmpty(txtEndDate.Text))
                {
                    MessageBox.Show("请选择外出结束日期");
                    return;
                }
                if (txtEndTime.Value == null)
                {
                    MessageBox.Show("请选择外出结束时间");
                    return;
                }
                if (cbLeaveType.SelectedValue == null)
                {
                    MessageBox.Show("请选择外出类型");
                    return;
                }

                if (string.IsNullOrEmpty(txtReason.Text))
                {
                    MessageBox.Show("请填写外出理由");
                    return;
                }
                if (empInfo != null)
                {
                    GoOutMgrInfo model = new GoOutMgrInfo();
                    model.ID = Guid.NewGuid().ToString();
                    model.EmployeeID = empInfo.ID;
                    model.KType = int.Parse(cbLeaveType.SelectedValue.ToString());
                    model.StartTime = DateTime.Parse(txtBeginDate.Text + " " + DateTime.Parse(txtBeginTime.Value.ToString()).ToLongTimeString());
                    model.EndTime = DateTime.Parse(txtEndDate.Text + " " + DateTime.Parse(txtEndTime.Value.ToString()).ToLongTimeString());
                    model.KState = 0;
                    model.Reason = txtReason.Text;
                    model.CreateTime = DateTime.Now;
                    new BLL.GoOutMgr().Add(model);
                    MessageBox.Show("登记成功");
                    this.bindGoOutList();
                }
                else
                {
                    MessageBox.Show("请选择要登记的员工");
                }

            }
            catch (Exception ex)
            {
                Logger.Write("登记失败:" + ex.Message);
                MessageBox.Show("登记失败:" + ex.Message);
            }
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("确定退出吗？", "确定", MessageBoxButton.YesNo);
            if (result == MessageBoxResult.Yes)
            {
                this.Close();
            }
        }
    }
}
